package com.neusoft.kc.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

import com.neusoft.kc.model.Activity;  
import com.neusoft.kc.service.ActivityService;  
import com.neusoft.kc.utils.*;  


@Controller
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    /**
     * 新增
     */
    @ResponseBody
    @RequestMapping(value="/insert",method=RequestMethod.POST)
    public Result<String> insert(@RequestBody Activity activity){
        int res=activityService.insert(activity);
        if(res==1){
            return ResultGenerator.genSuccessResult();
        }else{
        	return ResultGenerator.genFailResult(null);
        }
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping(value="/delete/{ids}",method=RequestMethod.GET)
    public Result<String> delete(@PathVariable("ids") String ids){
    	if(null==ids||ids.equals("")){
    		return ResultGenerator.genFailResult(null);
    	}
        String str[]=ids.split(",");
        for(String s:str){
            activityService.deleteByPrimaryKey(Integer.valueOf(s));
        }
        return ResultGenerator.genSuccessResult();
    }
    
    /**
     * 更新
     */
    @ResponseBody
    @RequestMapping(value="/update",method=RequestMethod.POST)
    public Result<String> update(@RequestBody Activity activity){
    	int res=activityService.updateByPrimaryKey(activity);
    	if(res==1){
    		return ResultGenerator.genSuccessResult();
    	}else{
    		return ResultGenerator.genFailResult(null);
    	}
    }
    
    /**
     * 主键查询
     */
    @ResponseBody
    @RequestMapping(value="/info/{id}",method=RequestMethod.GET)
    public Result<?> selectById(@PathVariable("id") int id){
    	Activity activity = activityService.selectByPrimaryKey(id);
    	if(null==activity){
    		return ResultGenerator.genFailResult(null);
    	}else{
    		return ResultGenerator.genSuccessResult(activity);
    	}
    }
    
    /**
     * 条件和排序的分页查询
     */
    @ResponseBody
    @RequestMapping(value="/search",method=RequestMethod.POST)
    public Map<String,Object> search(
    		@RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "rows", required = false) String rows,
            Activity activity,HttpServletResponse response){
    	//设置分页参数
    	Map<String, Object> map = new HashMap<String, Object>();
        if (page != null && rows != null) {
            PageBean pageBean = new PageBean(Integer.parseInt(page),
                    Integer.parseInt(rows));
            map.put("start", pageBean.getStart());
            map.put("size", pageBean.getPageSize());
        }
        
        
        
        //设置排序参数
        map.put("sort","id");
        
        //发送数据
        List<Activity> list=activityService.select(map);
        int total=activityService.count(map);
        
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("rows", list);
        resultMap.put("total", total);
        return resultMap;
    }
}